﻿/*-----------------------------------------------
// File: UltimateBeforeState.cs
// Description: 大招前的准备阶段, 1:等待其他人物放完技能 2:放大招的人走位，摄像机转动 3：大招前立绘出现
// Author: Shaobing	492057342@qq.com
-----------------------------------------------*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace Battle.View
{
    public class UltimateBeforeState : Fsm.FsmStateBase<BattleRoundState>
    {
        BattleRoundCtrl battleRoundCtrl
        {
            get { return this.controller as BattleRoundCtrl; }
        }
        public UltimateBeforeState(BattleRoundState state, BattleRoundCtrl ctrl) : base(state, ctrl) { }


        UltimateBehaviourPackage currentUltimatePackage
        {
            get { return BattleViewCtrl.GetInstance().GetUltimatePackage(); }
        }

        float delatTime = 0f;

        #region MoveBackAndCameraShow
        BattleCharacter sourceBC
        {
            get { return battleRoundCtrl.GetSource(); }
        }
        internal void InitMoveBackAndCameraShow()
        {
            battleRoundCtrl.ClearUltimateCahce();
            battleRoundCtrl.InitBCs(currentUltimatePackage);
            battleRoundCtrl.SetStateCaches();
            sourceBC.SwitchState(ActorState.Ultimate);
            foreach (var kvp in BattleViewCtrl.GetInstance().GetAllActors())
            {
                if (kvp.Value == sourceBC)
                    continue;
                kvp.Value.gameObject.SetActive(false);
            }
            CameraController.Instance.Ultimate(sourceBC.camp, sourceBC.GetBeFoundBonePosition(), sourceBC.height);
            CameraController.Instance.FocusCharactor(sourceBC);
            delatTime = 0f;
        }

        #endregion

        public const float ultiPaintShowTime = 1f;

        #region ShowPaint
        float ShowPaintTime
        {
            get
            {
                return Dict.Blo.DictConfigBlo.GetUltimateMoveBackTime() - ultiPaintShowTime;
            }
        }

        bool isPaintShowed = false;
        internal void InitShowPaint()
        {
            // 可以显示男女主人公的大招立绘！
            //if (GameConfig.Instance.GetChannelId == ChannelId.Qihu)
            //{
            //    if (sourceBC.heroClass == (int)HeroClass.Main_Actor_Man || sourceBC.heroClass == (int)HeroClass.Main_Actor_Woman)
            //    {
            //        return;
            //    }
            //}

            var skill = sourceBC.GetSkill(SkillType.Ultimate) as UltimateSkill;
            if (skill != null && !string.IsNullOrEmpty(skill.ultiPaintResUrl))
            {
                BattleManager.Instance.battlePanel.ShowUltimatePaint(skill.ultiPaintResUrl);
            }
        }
        #endregion


        float ultiBeforeTime
        {
            get
            {
                return Dict.Blo.DictConfigBlo.GetUltimateMoveBackTime();
            }
        }
        public override void Enter()
        {
            Debug.Log("------------------UltimateBeforeState Enter");
            isTimeout = false;
            InitMoveBackAndCameraShow();
            isPaintShowed = false;
        }

        public void Skip()
        {
            var sourceBC = BattleViewCtrl.GetInstance().GetBattleCharacter(currentUltimatePackage.actorUid);
            var currentSkill = sourceBC.GetSkill(SkillType.Ultimate) as UltimateSkill;

            BattleViewCtrl.GetInstance().AddAnger(-Dict.Blo.DictConfigBlo.GetUltimateCostAngerNum(), sourceBC.camp);
            BattleManager.Instance.battlePanel.HideUltimatePaint();
            
            CameraController.Instance.ReadyForUltimateEnd(currentSkill.cameraEndType);

            BattleViewCtrl.GetInstance().DoneUltimateBehaviour();
            var bcState = sourceBC.GetCurrentState() as BCUltimateState;
            if (bcState != null)
            {
                bcState.Skip();
            }

            foreach (var kvp in BattleViewCtrl.GetInstance().GetAllActors())
            {
                if (kvp.Value == sourceBC)
                {
                    continue;
                }
                kvp.Value.gameObject.SetActive(true);
            }

            battleRoundCtrl.SwitchState(BattleRoundState.UltimateEndState);
        }


        bool CheckUltimateResource()
        {
            return sourceBC.UltimateFull();
        }

        bool isTimeout = false;

        public override void Update()
        {
            delatTime += Time.deltaTime;
            if (delatTime >= ShowPaintTime && !isPaintShowed)
            {
                InitShowPaint();
                isPaintShowed = true;
            }

            if (delatTime >= ultiBeforeTime && !isTimeout)
            {
                Next();
                isTimeout = true;
            }

        }

        void Next()
        {
            if (!CheckUltimateResource() || IsAutoSkipUltimate())
            {
                BattleViewCtrl.GetInstance().AskJumpUltimate();
            }
            else
            {
                battleRoundCtrl.SwitchState(BattleRoundState.UltimateState);
            }
        }

        bool IsAutoSkipUltimate()
        {
            return StaticData.isAutoSkipUltimate && BattleViewCtrl.GetInstance().isAuto;
        }

        public override void Leave()
        {
            CameraController.Instance.UnFocusCharactor(sourceBC);
        }
    }
}
